﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sodao.Demo.Core.Configuration
{
    public class AutoFacManage
    {
        private readonly static ContainerBuilder builder = new ContainerBuilder();

        public void Init()
        {
            Build();
        }

        private static IContainer container;

        public void Register(Action<ContainerBuilder> action)
        {
            action(builder);
        }

        public T GetService<T>()
        {
            return container.Resolve<T>();
        }

        public T GetService<T>(string name, object obj)
        {
            return container.Resolve<T>(new NamedParameter(name, obj));
        }
        public T GetService<T>(IDictionary<string, object> dict)
        {
            if (dict == null || dict.Count == 0)
            {
                return GetService<T>();
            }
            NamedParameter[] list = new NamedParameter[dict.Count];
            int i = 0;
            foreach (var m in dict)
            {
                list[i++] = new NamedParameter(m.Key, m.Value);
            }
            return container.Resolve<T>(list);
        }

        private void Build()
        {
            container = builder.Build();
        }

    }
}
